package com.jbp.controller;

import java.util.*;
import java.util.stream.Collectors;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.model.Gift;
import com.jbp.model.UserPacksack;
import com.jbp.service.GiftService;
import com.jbp.service.UserPacksackService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.jbp.utils.PageUtils;
import com.jbp.utils.R;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 背包表
 *
 * @author lucheng feng
 * @email retailers@gmail.com
 * @date 2020-06-26 13:49:51
 */
@Api(tags ="APP接口--用户背包相关的" )
@ApiIgnore
@RestController
@RequestMapping("/rest/userpacksack")
public class UserPacksackController {
    @Autowired
    private UserPacksackService userPacksackService;

    @Autowired
    private GiftService giftService;

    /**
     * 列表
     */
    @ApiOperation(value="背包列表",notes="背包列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = UserPacksack.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "token",paramType = "query"),
            @ApiImplicitParam(name="sessionId",value = "sessionId",paramType = "query"),
    })
    @GetMapping("/list")
    public R list(@RequestAttribute("userId") Integer userId){
        List<UserPacksack> list= userPacksackService.list(new QueryWrapper<UserPacksack>().eq("user_id",userId));
        if (list == null || list.size() < 1){
            return R.ok().put("data",new ArrayList<UserPacksack>());
        }
        list.forEach( li -> {
           Gift gift= giftService.getById(li.getGiftId());
           li.setPrice(gift.getCoin());
            li.setGiftName(gift.getName());
            li.setGiftImage(gift.getImgUrl());
            li.setPrice(gift.getCoin());
        });

        List<UserPacksack> result = list.stream().sorted(Comparator.comparingInt(UserPacksack::getPrice)).collect(Collectors.toList());
        return R.ok().put("data", result);
    }

    /**背包送礼 */
    @ApiOperation(value="背包送礼")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Void.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomId",value = "房间Id", paramType = "body"),
            @ApiImplicitParam(name = "giftId",value = "礼物Id", paramType = "body"),
            @ApiImplicitParam(name = "otherId",value = "多个userId用逗号,连接", paramType = "body"),
            @ApiImplicitParam(name = "num",value = "礼物数量", paramType = "body"),
    })
    @PostMapping("/sendPackGift")
    public synchronized R sendPackGift(@RequestBody Map<String, Object> params, @RequestAttribute("userId") Integer userId){
        if (params.get("roomId")==null || params.get("roomId").equals("")
                || params.get("giftId")==null || params.get("giftId").equals("")
                || params.get("otherId")==null || params.get("otherId").equals("")) {
            return R.error("参数异常");
        }
        params.put("userId",userId);
        userPacksackService.sendPackGift(params);
        return R.ok();
    }

    /**背包全送 */
    @ApiOperation(value="背包全送", notes = "只能送给一个人")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Void.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomId",value = "房间Id", paramType = "body"),
            @ApiImplicitParam(name = "otherId",value = "userId", paramType = "body"),
    })
    @PostMapping("/sendAllPackGift")
    public synchronized R sendAllPackGift(@RequestBody Map<String, Object> params, @RequestAttribute("userId") Integer userId){
        if (params.get("roomId")==null || params.get("roomId").equals("")
                || params.get("otherId")==null || params.get("otherId").equals("")) {
            return R.error("参数异常");
        }
        params.put("userId",userId);
        userPacksackService.sendAllPackGift(params);
        return R.ok();
    }

}
